import axios from "axios";
import { createBrowserHistory } from 'history'
var history = createBrowserHistory();

axios.interceptors.request.use((config) => {
    config.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token');
    return config;
}, (error)=> { return Promise.reject(error) })


axios.interceptors.response.use((res)=>{
    return res
},(error) => {
    if(error.response.status==401){
        alert('身份验证失败，重新登录')
        localStorage.removeItem('token')
        history.push('/login');
        history.go()
    }else if(error.response.status==404){
        alert('访问路径错误')
    }else if(error.response.status==500){
        alert('服务器内部错误')
    }
    return Promise.reject(error)
})

export default axios;